//
//  CharlesViewController.m
//  Charles＋SourceTree测试Demo1
//
//  Created by zhang on 16/7/7.
//  Copyright © 2016年 zhangdefu. All rights reserved.
//

#import "CharlesViewController.h"
#import "UserInformationModel.h"

@interface CharlesViewController ()

@property (weak, nonatomic) IBOutlet UILabel *nickNameLable;
@property (weak, nonatomic) IBOutlet UILabel *qiandaoLabel;
@property (weak, nonatomic) IBOutlet UILabel *shuquanLabel;
@property (weak, nonatomic) IBOutlet UILabel *jifenLabel;
@property (weak, nonatomic) IBOutlet UIImageView *touxiangView;

@end

@implementation CharlesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)getUserInformationBtn:(id)sender {

    // 1.确定请求路径
    NSURL *url = [NSURL URLWithString:@"http://192.168.99.0:8080/json"];
    // 2.创建请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 3.发送get请求
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
        NSDictionary *userInformation = [NSJSONSerialization JSONObjectWithData:data  options:NSJSONReadingMutableLeaves error:&connectionError];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            // 昵称
            self.nickNameLable.text = [userInformation objectForKey:@"nickName"];
            
            // 书券
            float shuquan = [[userInformation objectForKey:@"shuquan"] floatValue];
            self.shuquanLabel.text =  [NSString stringWithFormat:@"%f",shuquan];
            
            // 积分
            float jifen = [[userInformation objectForKey:@"jifen"] floatValue];
            self.jifenLabel.text =  [NSString stringWithFormat:@"%f",jifen];
            
            // 签到
            int qiandao = [[userInformation objectForKey:@"qiandao"] intValue];
            self.qiandaoLabel.text =  [NSString stringWithFormat:@"%d",qiandao];
            
            // 头像
            NSString *touxiang = [userInformation objectForKey:@"touxiang"];
            NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:touxiang] options:kNilOptions error:nil];
            self.touxiangView.image = [UIImage imageWithData:data1];
        });
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
